package com.example.renshiguanli.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.renshiguanli.config.PaginationConstant;
import com.example.renshiguanli.mapper.AppraiseMapper;
import com.example.renshiguanli.mapper.EmpMapper;
import com.example.renshiguanli.pojo.Appraise;
import com.example.renshiguanli.pojo.Emp;
import com.example.renshiguanli.service.AppraiseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//绩效管理

@Controller
public class AppraiseController {
    @Autowired
    private AppraiseService appraiseService;
    @Autowired
    private AppraiseMapper appraiseMapper;
    @Autowired
    private EmpMapper empMapper;

    /**
     * 查询所有信息
     * @param model
     * @param pageNum
     * @return
     */
    @GetMapping("/appraises")
    public String findAll(Model model, @RequestParam(value="pageNum",defaultValue="1") Integer pageNum){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum= PaginationConstant.CURRENT_NUM;
        }
        //设置分页(当前页，和每页显示数据条数)
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
//        查询数据
        List<Appraise> all = appraiseService.findAll();
        model.addAttribute("appraises",all);
        PageInfo<Appraise> pageInfo=new PageInfo<>(all);
        model.addAttribute("pageInfo",pageInfo);
        return "appraise/appraise";
    }
    @GetMapping("/appraise")
    public String findAppraise(Model model){
//        展示员工姓名信息
        List<Emp> emps = empMapper.getAllEmps();
        model.addAttribute("emps",emps);
        return "appraise/add";
    }
//    添加绩效评估
    @PostMapping("/appraise")
    public String addAppraise(Appraise appraise){
        appraiseMapper.add(appraise);
        return "redirect:/appraises";
    }
//    编辑绩效评估信息
//    根据id查询信息
    @GetMapping("/appraise/{id}")
    public String findId(@PathVariable("id") Integer id,Model model){
        Appraise byId = appraiseMapper.findById(id);
        model.addAttribute("appraise",byId);
        List<Emp> allEmps = empMapper.getAllEmps();
        model.addAttribute("emps",allEmps);
        return "appraise/add";
    }
//    修改信息
    @PutMapping("/appraise")
    public String update(Appraise appraise){
        appraiseMapper.updateByAppraise(appraise);
        return "redirect:/appraises";
    }
//    删除信息
    @DeleteMapping("appraise/{id}")
    public String delete(@PathVariable Integer id){
        appraiseMapper.deleteByAppraise(id);
        return "redirect:/appraises";
    }
//    搜索信息
    @PostMapping("/appsearch")
    public String appsearch(String name,@RequestParam(value="pageNum",defaultValue="1") Integer pageNum,Model model){
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
//        查找数据
        List<Appraise> query = appraiseService.query(name);
        model.addAttribute("appraise",query);
        PageInfo<Appraise> pageInfo=new PageInfo<>(query);
        model.addAttribute("pageInfo",pageInfo);
        return "appraise/appraise";
    }
}
